'''
Created on Jul 9, 2011

@author: yc30v1m
'''
# used by TL Attachments 
from ConfigParser import ConfigParser
from StringIO import StringIO
from library.AutoLoad import AutoLoad

class Attachment(object):
    '''
    classdocs
    '''
    _raw_contents = ""
    _file_contents = ""
    _flag = False
    
    _configParser = ""
    _testcase_section_name = 'script'
    _tcid_key = 'TESTCASE_ID'
    _options = ['TCID','TESTCASE_ID','ATTR_NAME','TYPE','FULL_FILE_NAME','FILE_NAME','DELIMITER','HEADER','PRIMARY_COLUMN','VALIDATE_COLUMN','DATABASE','QUERY','QUERY_REPLACE_TEXT','REPLACE_WITH_COLUMN']
    _testCase = {}

    def __init__(self, testScriptFileContents):
        self._configParser = ConfigParser()
        if len(testScriptFileContents) > 0:
            self._raw_contents = testScriptFileContents
            self._contents()
            self.__processTestCaseId()
            
        
    
    def getProcessed(self):
        if self._flag:
            return self._testCase
        return False
    
    def _processTestCaseId(self):
        tcid = self._getSectionAndOption(self._testcase_section_name, self._tcid_key)
        tc = {}
        for attr in self._options:
            val = False
            val = self._getSectionAndOption(self._testcase_section_name, attr)
            if val != False:
                tc[attr] = val
        if tcid != False:
            self._testCase[tcid] = tc
        else:
            self._flag = False
        return self._testCase
    
    def __processTestCaseId(self):
        for attr in self._options:
            val = False
            val = self._getSectionAndOption(self._testcase_section_name, attr)
            if val != False:
                self._testCase[attr] = val
                self._flag = True
            # End If
        # End For
        return self._testCase
    
    def _contents(self):
        #Convert the string to FileLike object for ConfigParser
        try:
            _testCaseScript = StringIO(self._raw_contents)
            self._file_contents = self._configParser.readfp(_testCaseScript)
            self._flag = True
        except:
            self._flag = False
            AutoLoad.logExceptionInfo(10)
        
    def _getSectionAndOption(self,section, option):
        if self._configParser.has_option(section, option)== True:
            return self._configParser.get(section,option)
        return False